﻿using System.Collections.Generic;
using System.Drawing;
using BlizzardApi.Model;
using BlizzardApi.Services;

namespace BlizzardApi
{
    public interface IWowService
    {
        IEnumerable<Race> GetRaces();
        IEnumerable<Class> GetClasses();
        IEnumerable<GuildReward> GetGuildRewards();
        IEnumerable<GuildPerk> GetGuildPerks();
        IEnumerable<ItemClass> GetItemClasses();
        IEnumerable<RealmStatus> GetRealmStatuses();
        IEnumerable<AuctionFile> GetAuctionFiles(string realmSlug);
        AuctionHouseCollection GetAuctionHouses(AuctionFile auctionFile);
        Character GetCharacter(string realmSlug, string characterName);
    	IEnumerable<AchievementCategory> GetGuildAchievementCategories();
		IEnumerable<AchievementCategory> GetCharacterAchievementCategories();
        Guild GetGuild(string realmSlug, string guildName);
        Item GetItem(int id);
        Image GetIcon(IconSize iconSize, string iconName);
    }

    public class WowService : IWowService
    {
        private readonly IWebRequestor _webRequestor;
        private readonly IResponseParser _responseParser;

        public WowService(IWebRequestor webRequestor, IResponseParser responseParser)
        {
            _webRequestor = webRequestor;
            _responseParser = responseParser;
        }

        private T Get<T>(string url, params object[] args)
        {
            return _responseParser.Parse<T>(_webRequestor.GetString(string.Format(url, args)));
        }

        public IEnumerable<Race> GetRaces()
        {
            return Get<RaceCollection>("api/wow/data/character/races").Races;
        }

        public IEnumerable<Class> GetClasses()
        {
            return Get<ClassCollection>("api/wow/data/character/classes").Classes;
        }

        public IEnumerable<GuildReward> GetGuildRewards()
        {
            return Get<GuildRewardCollection>("api/wow/data/guild/rewards").Rewards;
        }

        public IEnumerable<GuildPerk> GetGuildPerks()
        {
            return Get<GuildPerkCollection>("api/wow/data/guild/perks").Perks;
        }

        public IEnumerable<ItemClass> GetItemClasses()
        {
            return Get<ItemClassCollection>("api/wow/data/item/classes").Classes;
        }

        public IEnumerable<RealmStatus> GetRealmStatuses()
        {
            return Get<RealmStatusCollection>("api/wow/realm/status").Realms;
        }

        public IEnumerable<AuctionFile> GetAuctionFiles(string realmSlug)
        {
            return Get<AuctionFileCollection>("api/wow/auction/data/" + realmSlug).Files;
        }

        public AuctionHouseCollection GetAuctionHouses(AuctionFile auctionFile)
        {
            return Get<AuctionHouseCollection>(auctionFile.Url);
        }

        public Character GetCharacter(string realmSlug, string characterName)
        {
            return Get<Character>("api/wow/character/{0}/{1}?fields=guild,stats,talents,items,reputation,titles,professions,appearance,companions,mounts,pets,achievements,progression,pvp", realmSlug, characterName);
        }

        public IEnumerable<AchievementCategory> GetGuildAchievementCategories()
        {
            return Get<AchievementCategoryContainer>("api/wow/data/guild/achievements").Achievements;
        }

        public IEnumerable<AchievementCategory> GetCharacterAchievementCategories()
        {
            return Get<AchievementCategoryContainer>("api/wow/data/character/achievements").Achievements;
        }

        public Guild GetGuild(string realmSlug, string guildName)
        {
            return Get<Guild>("api/wow/guild/{0}/{1}?fields=members,achievements", realmSlug, guildName);
        }

        public Item GetItem(int id)
        {
            return Get<Item>("api/wow/item/{0}", id);
        }

        public Image GetIcon(IconSize iconSize, string iconName)
        {
            return Image.FromStream(_webRequestor.GetStream(string.Format("http://us.media.blizzard.com/wow/icons/{0}/{1}.jpg", (int)iconSize, iconName)));
        }
    }
}